package com.qumitech.visualnovel.service.exception.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.service.exception.ExceptionService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author ywxie
 * @date 2019/11/27 18:02
 * @description
 */
@Service
public class ExceptionServiceImpl implements ExceptionService {

    @Override
    public void getFailureResultEnum(BaseRsp rsp) {
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            if (rsp == null) {
                throw new BusinessException(ResultEnum.FAILURE);
            }
            Map<String, Object> rspMap = (Map<String, Object>) rsp.getError();
            ResultEnum resultEnum = ResultEnum.FAILURE;
            resultEnum.setMsg(rspMap.get("description").toString());
            resultEnum.setState(Integer.valueOf(rspMap.get("status").toString()));
            throw new BusinessException(resultEnum);
        }
    }
}
